#include "mygraphicsitem.h"

MyGraphicsItem::MyGraphicsItem()
{
    _leftHeight = 125;
    calculateLeftHeight();
    _rightHeight = 125;
    calculateRightHeight();
    _time = 100;
    _scaledTime = 100;
    _scale = 1;

    _color = new QColor(Qt::green);

    _type.append("NO_TYPE");

    setToolTip(QString("Kliknete tahnete\n a retkvicku vytahnete ;)"));
    setCursor(Qt::OpenHandCursor);
    setAcceptedMouseButtons(Qt::LeftButton);
}


QRectF MyGraphicsItem::boundingRect() const         //virtual
{
    return QRectF(0, 0,_scaledTime, LINE_HEIGHT);
}

void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)  //virtual
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setPen(*_color);
    painter->setBrush(QBrush(*_color, Qt::SolidPattern));

    QPoint *points = new QPoint[4];
    points[3] = QPoint(0,_scaledLeftHeight);
    points[2] = QPoint(0, LINE_HEIGHT);
    points[1] = QPoint(_scaledTime, LINE_HEIGHT);
    points[0] = QPoint(_scaledTime, _scaledRightHeight);
    painter->drawPolygon(points, 4);
}

void MyGraphicsItem::setLeftHeight(int height)
{
    _leftHeight = height;
    calculateLeftHeight();
}

void MyGraphicsItem::setRightHeight(int height)
{
    _rightHeight = height;
    calculateRightHeight();
}

void MyGraphicsItem::setTimeLenght(int time)
{
    _time = time;
    calculateTimeLenght();
}

int MyGraphicsItem::leftHeight() const
{
    return _leftHeight;
}

int MyGraphicsItem::rightHeight() const
{
    return _rightHeight;
}

int MyGraphicsItem::timeLenght() const
{
    return _time;
}

void MyGraphicsItem::setScaleX(qreal scale)
{
    _scale = scale;


    calculateScaledPosition();
    calculateTimeLenght();
}

qreal MyGraphicsItem::scaleX(void) const
{
    return _scale;
}
void MyGraphicsItem::setMyType(QString &type)
{
    _type = type;
}

QString MyGraphicsItem::myType() const
{
    return _type;
}

void MyGraphicsItem::setColor(QColor &color)
{
    *_color = color;
}

QColor* MyGraphicsItem::color(void) const
{
    return _color;
}

inline void MyGraphicsItem::calculateLeftHeight()
{
    _scaledLeftHeight = (LINE_HEIGHT - ((float)_leftHeight*((float)LINE_HEIGHT/255)));
}

inline void MyGraphicsItem::calculateRightHeight()
{
    _scaledRightHeight = (LINE_HEIGHT - ((float)_rightHeight*((float)LINE_HEIGHT/(float)255)));
}

inline void MyGraphicsItem::calculateTimeLenght()
{
    _scaledTime = _time * _scale;
}

void MyGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::ClosedHandCursor);
}

void MyGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
        .length() < QApplication::startDragDistance()) {
        return;
    }

    QDrag *drag = new QDrag(event->widget());
    QMimeData *mime = new QMimeData;
    drag->setMimeData(mime);
    mime->setText(QString("myItem"));
    mime->setData(QString("pointer"), QByteArray().setNum((qlonglong)this));
    mime->setData(QString("hotSpot"), QByteArray().setNum(event->buttonDownPos(Qt::LeftButton).x()));


    drag->exec(Qt::MoveAction);
}

void MyGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::OpenHandCursor);
}

void MyGraphicsItem::setXPos(qreal x)
{
    QPointF point = QGraphicsItem::pos();
    point.setX(x);
    setPos(point);
}

void MyGraphicsItem::setPos(qreal x, qreal y)
{
    setPos(QPointF(x,y));
}

void MyGraphicsItem::setPos(const QPointF &point)
{
    QGraphicsItem::setPos(point);
    _xPosition = point.x()/_scale;
}

int MyGraphicsItem::nonScaledXPosition(void) const
{
    return _xPosition;
}

int MyGraphicsItem::timeLenghtScaled() const
{
    return _scaledTime;
}

void MyGraphicsItem::calculateScaledPosition()
{
    QPointF rect;
    rect.setX(_xPosition *_scale);
    QGraphicsItem::setPos(rect);
}

void MyGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    ItemSetDialog dialog(this);
    dialog.show();
    dialog.exec();
}
